/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.controller.servlet;

import com.context.AppContext;
import com.controller.AbstractController;
import com.controller.ConglomeradoPersonalController;
import com.controller.ConglomeradoPersonalResumenController;
import com.controller.MonitoreoTransferenciaController;
import com.controller.PersonalController;
import com.controller.ReporteTransferenciaController;
import com.controller.UsuarioController;
import util.controller.Accion;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import util.controller.Vista;

/**
 *
 * @author Rdelacruz
 */
public class DispatcherExcelServlet extends HttpServlet {
    
    
    public DispatcherExcelServlet() throws Exception {
       
    }
    

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        
        //How to make excel file to be downloadable
        //http://stackoverflow.com/questions/6921923/how-to-make-excel-file-to-be-downloadable        
        response.setContentType("application/vnd.ms-excel");
        //response.setHeader("Content-disposition", "attachment;filename=miArchivo.xls");
        //String headerFilename = "attachment;filename";
        
        Vista v = new Vista();
        
        try {
            UsuarioController usuarioController = AppContext.getInstancia().getControllerContext().getUsuarioController();
            Accion accion = usuarioController.validarRequest(request,response);            

            //2. Luego despacha el request       
            String action = accion.getMetodo();
            AbstractController controller = accion.getControlador(); 
            
            request.setAttribute("excel", "S");
        
            if( controller instanceof ReporteTransferenciaController ) {
                ReporteTransferenciaController c = (ReporteTransferenciaController) controller;

                if(action.equals("buscarRecojoDatosResumen")) {
                    setHeaderFilename(response,"ReporteRecojoDatosResumen.xls");                           
                    c.buscarRecojoDatosResumen(request, response);                            
                }
                else if(action.equals("buscarRecojoDatosDetalle")) {
                    setHeaderFilename(response,"ReporteRecojoDatosDetalle.xls");                           
                    c.buscarRecojoDatosDetalle(request, response);                            
                }
                else if(action.equals("buscarEnviosViviendas")) {
                    //setHeaderFilename(response,"ReporteEnviosViviendas.xls");                           
                    //c.buscarEnviosViviendas(request, response);                            
                }
            }
            else if( controller instanceof MonitoreoTransferenciaController ) {
                MonitoreoTransferenciaController c = (MonitoreoTransferenciaController) controller;

                if(action.equals("buscarPorCapitulosResumen")) {
                    setHeaderFilename(response,"MonitoreoPorCapitulos_Resumen.xls");                           
                    c.buscarPorCapitulosResumen(request, response);
                }
                else if(action.equals("buscarPorCapitulosDetalle")) {
                    setHeaderFilename(response,"MonitoreoPorCapitulos_Detalle.xls");                           
                    c.buscarPorCapitulosDetalle(request, response);
                }
                else if(action.equals("buscarPorViviendas")) {
                    setHeaderFilename(response,"MonitoreoPorViviendas.xls");                           
                    c.buscarPorViviendas(request, response);
                }
                else if(action.equals("verReglasControlEnvios")) {
                    setHeaderFilename(response,"ReglasControlEnvios.xls");                           
                    c.verReglasControlEnvios(request, response);
                }
                else if(action.equals("buscarRecuperacionEnvios")) {
                    setHeaderFilename(response,"RecuperacionEnvios.xls");                           
                    c.buscarRecuperacionEnvios(request, response);
                }
            }
            else if( controller instanceof PersonalController ) {
                PersonalController c = (PersonalController) controller;
                if(action.equals("descargarArchivoExcel")) {
                    setHeaderFilename(response,"Personal.xls");
                    c.descargarArchivoExcel(v,request, response);
                }
                
            }            
            else if( controller instanceof ConglomeradoPersonalResumenController ) {
                ConglomeradoPersonalResumenController c = (ConglomeradoPersonalResumenController) controller;
                if(action.equals("buscarReporte")) {
                    setHeaderFilename(response,"ReporteAsignacionPartidas.xls");
                    c.buscarReporte(v,request, response);
                }
            }
            /*
            else if( controller instanceof ConglomeradoPersonalController ) {
                ConglomeradoPersonalController c = (ConglomeradoPersonalController) controller;
                if(action.equals("buscarResumen")) {
                    setHeaderFilename(response,"ResumenAsignacionPartidas.xls");
                    c.buscarResumen(v,request, response);
                }
            }
            */
        }
        catch(Exception e) {
            throw new ServletException(e);
        }          
        
         String url = v.getUrl();
        if(url!=null)
            request.getRequestDispatcher(url).forward(request, response);
        
    }
    
    private void setHeaderFilename(HttpServletResponse response, String nombreArchivo) {
        response.setHeader("Content-disposition", "attachment;filename="+nombreArchivo);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
